package com.lc.index.utils;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
//import net.sf.jasperreports.engine.*;
//import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Slf4j
public class JasperHelper {
/*

    //必填
    private HttpServletResponse response;
    private String jrxmlTemplate;
    private Map<String, Object> params;

    //选填
    private boolean downLoad;
    private Connection conn;
    private List<?> data;
    private String fileName;

    public JasperHelper(Builder builder) {
        this.response = builder.response;
        this.jrxmlTemplate = builder.jrxmlTemplate;
        this.params = builder.params;
        this.downLoad = builder.downLoad;
        this.conn = builder.conn;
        this.data = builder.data;
        this.fileName = builder.fileName;
    }

    public static class Builder {
        //必填
        private HttpServletResponse response;
        private String jrxmlTemplate;
        //选填
        private Map<String, Object> params;
        private Connection conn;
        private List<?> data;
        private String fileName;
        private boolean downLoad;

        */
/**
         * 使用报表内部的sql查询数据
         *
         * @param response
         * @param jrxmlTemplate jrxml文件名
         * @param conn
         *//*

        public Builder(HttpServletResponse response, String jrxmlTemplate, Connection conn) {
            this.response = response;
            this.jrxmlTemplate = jrxmlTemplate;
            this.downLoad = false;
            this.conn = conn;
        }

        */
/**
         * 给报表传数据显示
         *
         * @param response
         * @param jrxmlTemplate jrxml文件名
         * @param data
         *//*

        public Builder(HttpServletResponse response, String jrxmlTemplate, List<?> data) {
            this.response = response;
            this.jrxmlTemplate = jrxmlTemplate;
            this.downLoad = false;
            this.data = data;
        }

        public Builder params(Map<String, Object> params) {
            this.params = params;
            return this;
        }


        public Builder fileName(String fileName) {
            this.fileName = fileName;
            return this;
        }

        public Builder downLoad(boolean downLoad) {
            this.downLoad = downLoad;
            return this;
        }

        public JasperHelper build() {
            return new JasperHelper(this);
        }

    }

    public void exportPDF() throws IOException, JRException, SQLException {
        final String baseDir = "/ireport/";
        // 1. 获取模板文件
        JasperReport jasperReport;
        try {
            InputStream resourceAsStream = JasperHelper.class.getResourceAsStream(baseDir + jrxmlTemplate);
            // 2. 编译 jrxml
            jasperReport = JasperCompileManager.compileReport(resourceAsStream);
        } catch (Exception e) {
            File jasperFile = ResourceUtils.getFile("classpath:" + baseDir + jrxmlTemplate);
            // 2. 编译 jrxml
            jasperReport = JasperCompileManager.compileReport(jasperFile.getPath());
        }

        // 3. 获取报表
        JasperPrint jasperPrint = null;
        if (conn != null) {
            // 4. 获取数据源
            jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);
        } else {
            // 4. 获取数据源
            JRDataSource jrDataSource = new JRBeanCollectionDataSource(data);
            jasperPrint = JasperFillManager.fillReport(jasperReport, params, jrDataSource);
        }
        // 通过outputStream输出
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
        // 5. 设置响应格式
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/pdf");

        if (fileName == null) {
            fileName = FileUtil.mainName(jrxmlTemplate);
        }
        if (downLoad) {
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".pdf");
        } else {
            response.setHeader("content-disposition", "inline;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".pdf");
        }
        if (null != conn) {
            conn.close();
        }
    }
*/


}

